package com.yuantong.xyerp.rest.config.APIJson;

import apijson.framework.BaseModel;
import com.yuantong.xyerp.core.api.model.User;
import com.yuantong.xyerp.core.api.util.AesCipherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;

/***
 * 作用: 在这里定义Function函数, 然后 FunctionParser 直接调用返回
 * 行为:
 * @Author JianguoYin
 * @Date 上午11:19 2021/7/12
 **/
@Component
public class XyerpBaseModel extends BaseModel {

    private static JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        XyerpBaseModel.jdbcTemplate = jdbcTemplate;
    }

    /***
     * 作用: 测试 jdbcTemplate
     * 结果: 成功
     * @Date 下午2:55 2021/7/19
     **/
    public static int queryCount() {
        String sql = "select * from t_user";
        List<Map<String, Object>> users =jdbcTemplate.queryForList(sql);
        return users.size();
    }

    public static <T> long getNumber() {
        Random random = new Random();
        return random.nextLong();
    }

    /**判断map是否包含key
     * @param <K>
     * @param <V>
     * @param map
     * @param key
     * @return
     */
    public static <K, V> boolean isContainKey(Map<K, V> map, K key) {
        return map != null && map.containsKey(key);
    }

    /***
     * 作用: 返回加密后的密码
     * @Date 下午2:46 2021/7/13
     **/
    public static String enCrypto(String var1) {
        return AesCipherUtil.enCrypto(var1);
    }
    public static boolean test(double amount,String accountId){
        String sql = "update t_account set current_balance =current_balance + ? where id = ?";
        int count = jdbcTemplate.update(sql,amount,accountId);
        if (count<1) return false;
        return true;
    }
}
